/**
 * 部门管理js
 * 
 * @returns
 */
$(function() {
	initialPage();
	getGrid();
});

/**
 * 初始化表格
 */
var TreeGrid = {
	id : "dataGrid",
	table : null,
	layerIndex : -1
}

TreeGrid.initColumn = function() {
	var columns = [ {
		field : 'selectItem',
		radio : true
	}, {
		title : '部门编码',
		field : 'depart_code',
		align : 'center',
		valign : 'middle',
		width : '50px'
	}, {
		title : '部门名称',
		field : 'name',
		align : 'center',
		valign : 'middle',
		width : '100px'
	}, {
		title : '上级部门',
		field : 'parentName',
		align : 'center',
		valign : 'middle',
		width : '100px'
	}, {
		title : '状态',
		field : 'depart_state',
		align : 'center',
		valign : 'middle',
		width : '100px',
		formatter : function(item, index) {
			if (item.depart_state == 1) {
				return '<i class="fa fa-toggle-off"></i>';
			}
			if (item.depart_state == 0) {
				return '<i class="fa fa-toggle-on"></i>';
			}
		}
	} ]
	return columns;
};

/**
 * 加载TreeGrid高度
 * 
 * @returns
 */
function initialPage() {
	$(window).resize(function() {
		TreeGrid.table.resetHeight({
			height : $(window).height() - 124
		});
	});
}

function getGrid() {
	var colunms = TreeGrid.initColumn();
	var table = new TreeTable(TreeGrid.id, '../../sys/depart/list?_' + $.now(),
			colunms);
	table.setExpandColumn(2);
	table.setIdField("depart_code");
	table.setCodeField("depart_code");
	table.setParentCodeField("parent_code");
	table.setExpandAll(false);
	table.setHeight($(window).height() - 100);
	table.init();
	TreeGrid.table = table;
}

var vm = new Vue(
		{
			el : '#ltLTE',
			methods : {
				load : function() {
					TreeGrid.table.refresh();
				},
				save : function() {
					dialogOpen({
						title : '新增部门',
						url : 'base/department/add.html?_' + $.now(),
						width : '620px',
						height : '420px',
						scroll : false,
						yes : function(iframeId) {
							top.frames[iframeId].vm.acceptClick();
						}
					});
				},
				edit : function() {
					var ck = TreeGrid.table.getSelectedRow();
					if (checkedRow(ck)) {
						dialogOpen({
							title : '编辑部门',
							url : 'base/department/edit.html?_' + $.now(),
							width : '600px',
							height : '420px',
							scroll : false,
							success : function(iframeId) {
								top.frames[iframeId].vm.department.depart_code = ck[0].id;
								top.frames[iframeId].vm.setForm();
							},
							yes : function(iframeId) {
								top.frames[iframeId].vm.acceptClick();
							}
						});
					}
				},
				remove : function() {
					var ck = TreeGrid.table.getSelectedRow(), depart_code = null;
					if (checkedRow(ck)) {
						depart_code = ck[0].id;
						$.RemoveForm({
							url : '../../sys/depart/remove?_' + $.now(),
							param : depart_code,
							success : function(data) {
								vm.load();
							}
						});
					}
				}
			}
		});
